/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import java.applet.Applet;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Brian
 */
public class backupdb{
    private static ResultSet res;
    private static Connection con;
    private static Statement st;
    private static int BUFFER = 10485760;
    public static void backupDB(String hostname,String dbName, String dbUserName, String dbPassword) throws IOException, InterruptedException {

        //String executeCmd = "mysqldump --host="+hostname+ " -u " + dbUserName + " -p" + dbPassword +" "+ dbName + " -r " + path;
//        String[] executeCmd = new String[]{"mysql", "--host=" + hostname, "--user=" + dbUserName, "--password=" + dbPassword, dbName,"-e", "source "+path};
        String executeCmd = "/Applications/MySQLWorkbench.app/Contents/Resources/mysqldump --host=" + hostname+ " --port=3306 --user=" + dbUserName+ " --password=" + dbPassword+
           " --compact --databases --add-drop-table --complete-insert --extended-insert " +
           "--skip-comments --skip-triggers "+dbName;
        Process runtimeProcess;
        try {
//      
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup created successfully");
            } else {
                System.out.println("Could not create the backup");
            }
        
        InputStream in = runtimeProcess.getInputStream(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        StringBuffer temp = new StringBuffer();
        int count;
         char[] cbuf = new char[BUFFER];

        while ((count = br.read(cbuf, 0, BUFFER)) != -1)
                 temp.append(cbuf, 0, count);

         br.close();
         in.close();
         
         
         String desktopPath = System.getProperty("user.home") + "/Desktop";
         File backupFile = new File(desktopPath+"/backupMySQL.sql");
         FileOutputStream dest = new FileOutputStream(backupFile);
                dest.write(temp.toString().getBytes());
                
          dest.close();
        
        
        } catch (Exception ex) {
            executeCmd = getMysqlBinPath()+"mysqldump --host=" + hostname+ " --port=3306 --user=" + dbUserName+ " --password=" + dbPassword+
           " --compact --databases --add-drop-table --complete-insert --extended-insert " +
           "--skip-comments --skip-triggers "+dbName;
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup created successfully");
            } else {
                System.out.println("Could not create the backup");
            }
        
        InputStream in = runtimeProcess.getInputStream(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        StringBuffer temp = new StringBuffer();
        int count;
         char[] cbuf = new char[BUFFER];

        while ((count = br.read(cbuf, 0, BUFFER)) != -1)
                 temp.append(cbuf, 0, count);

         br.close();
         in.close();

          String desktopPath = System.getProperty("user.home") + "/Desktop";
         File backupFile = new File(desktopPath+"/backupMySQL.sql");
         FileOutputStream dest = new FileOutputStream(backupFile);
                dest.write(temp.toString().getBytes());
                
          dest.close();
        }
    }
    
    /*Function to find MySql Path*/

    public static String getMysqlBinPath() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:mysql://mysql1.cs.stonybrook.edu:3306/brchung","brchung","106747535");
            st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        } catch (Exception e) {

            e.printStackTrace();
        }
        String a = "";
        try {
            res = st.executeQuery("select @@basedir");
            while (res.next()) {
                a = res.getString(1);
            }
        } catch (Exception eee) {
            eee.printStackTrace();
        }
        a = a + "bin\\";
        System.err.println("Mysql path is :" + a);
        return a;
    }
   
    
    public static String test(){
        return getMysqlBinPath();
    }
}